package com.zmzncs.lmtc.module.promotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.RuleStatusEnum;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_fee_scale.ReserveFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_fee_scale.ReserveFeeScaleVO;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule.*;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule_park.ReserveRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.reserve_rule_park.ReserveRuleParkVO;
import com.zmzncs.lmtc.module.promotion.service.ReserveFeeScaleService;
import com.zmzncs.lmtc.module.promotion.service.ReserveRuleParkService;
import com.zmzncs.lmtc.module.promotion.service.ReserveRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 促销-预约规则表 前端控制器
 * </p>
 *
 * @author 高博文
 * @since 2020-12-14
 */
@Api(tags = "预约规则")
@RestController
@RequestMapping("/promotion/reserve-rule")
public class ReserveRuleController {

    @Autowired
    ReserveRuleService reserveRuleService;
    @Autowired
    ReserveRuleParkService reserveRuleParkService;
    @Autowired
    ReserveFeeScaleService reserveFeeScaleService;


    @ApiOperation(value = "添加预约规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "addReserveRule")
    public Rest addReserveRule(@RequestBody @Validated ReserveRuleAddForm form) {
        form.validate();
        reserveRuleService.addReserveRule(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询预约规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="pageNum", value="页码", required = true),
            @ApiImplicitParam(paramType="query", name="pageSize", value="页长", required = true),
    })
    @GetMapping(value = "getReserveRulePage")
    public Rest<IPage<ReserveRuleTableVO>> getReserveRulePage(@NotNull Integer pageNum, @NotNull Integer pageSize) {
        IPage<ReserveRuleDTO> reserveRuleDTOIPage = reserveRuleService.getReserveRuleParkList(new Page<>(pageNum, pageSize));
        PageInfo<ReserveRuleTableVO> page = JsonUtil.pageTransform(reserveRuleDTOIPage, ReserveRuleTableVO.class);

        return RestUtil.success(page);
    }

    @ApiOperation(value = "开启/关闭规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "规则id", required = true),
            @ApiImplicitParam(paramType = "query", name = "status", value = "规则状态（1：开启，0：关闭）", required = true),
    })
    @PostMapping(value = "switchStatus")
    public Rest switchStatus(@NotNull Long id, @NotNull Integer status) {
        ReserveRule reserveRule = new ReserveRule();
        reserveRule.setId(id);
        reserveRule.setStatus(status);
        reserveRuleService.updateById(reserveRule);
        return RestUtil.success();
    }

    @ApiOperation(value = "删除规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "规则id", required = true),
    })
    @PostMapping(value = "deleteRule")
    public Rest deleteRule(@NotNull Long id) {
        ReserveRule reserveRule = new ReserveRule();
        reserveRule.setId(id);
        reserveRule.setIsDeleted(1);
        reserveRuleService.updateById(reserveRule);
        return RestUtil.success();
    }

    @ApiOperation(value = "根据id查询预约规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="id", value="id", required = true),
    })
    @GetMapping(value = "getReserveRuleById")
    public Rest<ReserveRuleInfoVO> getReserveRuleById(@NotNull Long id) {

        //查询预约规则
        ReserveRule reserveRule = reserveRuleService.getById(id);
        //  根据预约规则id查询停车场
        LambdaQueryWrapper<ReserveRulePark> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ReserveRulePark::getReserveRuleId, id);
        List<ReserveRulePark> reserveRuleParks = reserveRuleParkService.list(queryWrapper);
        List<ReserveRuleParkVO> reserveRuleParkVOS = JsonUtil.listTransform(reserveRuleParks, ReserveRuleParkVO.class);

        //  根据预约规则id查询收费标准
        LambdaQueryWrapper<ReserveFeeScale> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ReserveFeeScale::getReserveRuleId, id);
        List<ReserveFeeScale> reserveFeeScales = reserveFeeScaleService.list(lambdaQueryWrapper);
        List<ReserveFeeScaleVO> reserveFeeScaleVOS = JsonUtil.listTransform(reserveFeeScales, ReserveFeeScaleVO.class);

        //  设置返回类型
        ReserveRuleInfoVO reserveRuleInfoVO = new ReserveRuleInfoVO();
        BeanUtils.copyProperties(reserveRule, reserveRuleInfoVO);
        reserveRuleInfoVO.setDurationRuleParks(reserveRuleParkVOS);
        reserveRuleInfoVO.setFeeScaleList(reserveFeeScaleVOS);

        return RestUtil.success(reserveRuleInfoVO);
    }

    @ApiOperation(value = "修改预约规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "updateReserveRule")
    public Rest updateReserveRule(@RequestBody @Validated ReserveRuleUpdateForm form) {
        form.validate();

        reserveRuleService.updateReserveRule(form);
        return RestUtil.success();
    }



}

